function roundDecimal(num, decimalPlaces) {
  const factor = Math.pow(10, decimalPlaces) * 10 ;
  return Math.round(num * factor / 10) / (factor/10);
}
// npm i big.js
const Big = require('big.js')
const count = 10000000
function getNum(num) {
  const strArr = String(num).split('.')
  str = strArr[1] || ''
  return [Number(str[2]||0), Number(str[1]||0), Number(str[0]||0)]
}
for (let i = 0; i < count; i++) {
  // 生成一个两位数的随机数
  const num = Math.floor(Math.random() * 100)
  const time = Number(new Date().getTime()+ ''+ num + 5)  / 1000
  const Time = time
  const [last, calc,] = getNum(Time)
  const rounded2 = Big(time).toFixed(2)
  // const rounded2 = roundDecimal(time, 2);
  const [_, newLast] = getNum(rounded2)
  let flag = false;
  if (last >= 5) {
    switch (newLast) {
      case calc + 1:
        flag = true;
        break;
      case 0:
        if(calc === 9) {
          flag = true;
        }
    }
  }
  if (last < 5 && calc === newLast) {
    flag = true;
  }
  if (flag) {
    if (last === 4 || last === 5) {
      console.log('测试通过,该小数的值为：', time, '，四舍五入后的值为：', rounded2, '当前测试次数', i);
    } else {
      console.log(i, '测试通过')
    }
  } else {
    console.log('error小数运算错误, 该小数的值为：', time, '，四舍五入后的值为：', rounded2, '，测试失败', '当前测试次数', i);
    break;
  }
}
